﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using Follow.Utils;
using Follow.Model;
using System.Transactions;
using Follow.Common;

namespace Follow.DataAccess
{
    public class ItemPropostaDAO
    {
        private SqlDataReader reader = null;
        private SqlConnection conn = null;
        private SqlCommand cmd = null;

        public ItemPropostaDAO()
        {
            conn = ConnectionUtils.getConnection();
        }


        public IList<ItemProposta> findByProposta(Int32 id)
        {
            IList<ItemProposta> lista = new List<ItemProposta>();
            try
            {
                conn.Open();
                String sqlCommand = @" select [iditemproposta]
                                              ,[produto_idproduto]
                                              ,[proposta_idproposta]
                                              ,[quantidade]
                                              ,[valorunitario] from itemproposta where proposta_idproposta = " + id;

                cmd = new SqlCommand(sqlCommand, conn);
                reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    ItemProposta p = new ItemProposta();
                    p.IDItemProposta = reader.GetInt32(0);
                    p.IDProduto = reader.GetInt32(1);
                    p.IDProposta = reader.GetInt32(2);
                    p.Qtde = Convert.ToDecimal(reader[3]);
                    p.ValorUnit = Convert.ToDecimal(reader[4]);
                    lista.Add(p);
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (conn != null)
                {
                    conn.Close();
                }
            }
            return lista;
        }


        public void insert(ItemProposta ip, int idUsuario)
        {
            String sqlCommand;
            try
            {
                conn.Open();
                sqlCommand = (@"insert into itemproposta 
                                      ([produto_idproduto], [proposta_idproposta], [quantidade], [valorunitario]) 
                                      values 
                                      (@idproduto, @idproposta, @quantidade, @valorunitario) ");

                cmd = new SqlCommand(sqlCommand, conn);

                cmd.Parameters.Add(new SqlParameter("@idproduto", ip.IDProduto));
                cmd.Parameters.Add(new SqlParameter("@idproposta", ip.IDProposta));
                cmd.Parameters.Add(new SqlParameter("@quantidade", ip.Qtde));
                cmd.Parameters.Add(new SqlParameter("@valorunitario", ip.ValorUnit));
                cmd.ExecuteNonQuery();

            }
            catch (Exception ex)
            {

            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (conn != null)
                {
                    conn.Close();
                }
            }
            LogDAO log = new LogDAO();
            log.insert(idUsuario, Common.SQLStringSingleton.GetCommandText(cmd), "Insert", "Item Proposta");
        }

        public void delete(Int32 id, Int32 idlogin)
        {
            String sqlCommand;
            using (var transaction = new TransactionScope())
            {
                try
                {
                    conn.Open();
                    sqlCommand = " delete from itemproposta where iditemproposta = @iditemproposta ";
                    cmd = new SqlCommand(sqlCommand, conn);
                    cmd.Parameters.Add(new SqlParameter("iditemproposta", id));
                    cmd.ExecuteNonQuery();
                    transaction.Complete();
                }
                catch (Exception)
                {
                    transaction.Dispose();
                    throw;
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }

                    if (conn != null)
                    {
                        conn.Close();
                    }
                }
                LogDAO log = new LogDAO();
                log.insert(idlogin, SQLStringSingleton.GetCommandText(cmd), "Delete", "Item Proposta");
            }
        }


    }
}